package com.testing.login;

import com.alibaba.fastjson.JSONObject;
import com.testing.model.Result;
import com.testing.mysql.MysqlUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Classname ${NAME}
 * @Description 类型说明
 * @Date 2021/12/29 21:56
 * @Created by 特斯汀Roy
 */
@WebServlet
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String loginName = request.getParameter("user");
        String password = request.getParameter("password");
        System.out.println("传参的用户名是" + loginName);
        System.out.println("传参的密码是：" + password);

        //获取当前的session会话
        HttpSession nowSession = request.getSession();
        String sessionid = nowSession.getId();
        System.out.println("sessionid是：" + sessionid);


        //设计非法字符的正则表达式
        Pattern illW = Pattern.compile("[^\\w\\-_]");
        //用于完成用户名和密码的匹配器
        Matcher uMatcher = illW.matcher(loginName);
        Matcher pMatcher = illW.matcher(password);

        Result result = new Result();

        //每次调用接口的时候，先查询session中是否已经有记录了
        if (nowSession.getAttribute("user") == null) {
            if (loginName != null && loginName.trim().length() > 0 && password != null && password.trim().length() > 0) {
                if (!uMatcher.find() && !pMatcher.find()) {
                    MysqlUtils mu = new MysqlUtils();
                    mu.createConByProp();
                    //用数据库来完成查询验证操作
                    if (mu.loginCheck(loginName, password)) {
                        result.setStatus("200");
                        result.setMsg("恭喜您登录成功！");
                        //在session记录一下用户信息
                        nowSession.setAttribute("user", loginName);
                        //可以设置session的有效期，单位是秒
                        nowSession.setMaxInactiveInterval(120);

                        //发放cookie，设置JSESSIONID这个cookie的有效期。
                        //由于servlet默认发放JSESSIONID这个cookie，所以加一个新的进行覆盖即可。
                        Cookie jsessionId=new Cookie("JSESSIONID",sessionid);
                        jsessionId.setMaxAge(120);
                        //发放cookie
                        response.addCookie(jsessionId);

                    } else {
                        result.setStatus("-1");
                        result.setMsg("用户名密码错误！");
                    }
                } else {
                    result.setStatus("-2");
                    result.setMsg("输入了非法字符，请检查!");
                }
            } else {
                result.setStatus("-3");
                result.setMsg("用户名密码不能为空!");
            }
        } else {
            result.setStatus("-4");
            result.setMsg("已经有用户" + nowSession.getAttribute("user") + "登录过了，不能再次登录！");
        }
        String resultJson = JSONObject.toJSONString(result);
        System.out.println("结果字符串为" + resultJson);
        response.getWriter().append(resultJson);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取请求中user和password参数。
        String loginName = request.getParameter("user");
        String password = request.getParameter("password");
        if ("Roy".equals(loginName) && "123456".equals(password)) {
            response.getWriter().append("恭喜您登录成功");
        } else {
            response.getWriter().append("用户名密码错误！");
        }


        response.getWriter().append("第一次请求Get接口成功！");
    }
}
